<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String basePath = request.getScheme() + "://" + request.getServerName()
			+ ":" + request.getServerPort() + request.getContextPath();
%>
<jsp:include page="/WEB-INF/page/auth/_header.jsp"></jsp:include>
<body>
	<jsp:include page="/WEB-INF/page/auth/header.jsp"></jsp:include>
	<div id="middle">
		<jsp:include page="/WEB-INF/page/auth/menu.jsp"></jsp:include>
		<div id="right">
		    <div id="kccs_course_managetb">
		        <form id="kccs_course_manageForm" class="m10">
					课程名称:<input id="courseName" name="courseName" class="easyui-validatebox" />
		                             课程状态:<select id="isAgree" name="isAgree" class="form-control1 ">
                          	<option value="">请选择</option>
                          	<option value="0">待审批</option>
                          	<option value="1">同意</option>
                          	<option value="2">不同意</option>
		                  </select>
		            <a class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-search'" onclick="searchForm('kccs_course_manage')">查询</a>
		            <a class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-clear'" onclick="resetForm('kccs_course_manage')">重置</a>
		        	<a class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-save'" onclick="exportExcel('kccs_course_manage','<%=basePath%>/sys/course/manage/export')">导出为EXCEL</a>
		        	<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-large-smartart',plain:true" onclick="upload()">导入</a>
		        </form>
		    </div>
		    <table id="kccs_course_managedg">
		        <thead>
		            <tr>
		            	<th data-options="field:'id',checkbox:true"></th>
		            	<th data-options="field:'isAgree',title:'课程状态',
		            	editor:{
		            		type:'combobox',
							options: {  
			                    data: [{'value': '1', 'text': '同意'},{'value': '2', 'text': '不同意'}],  
			                    editable: false,  
			                    panelHeight:70,  
			                    required: true  
			                } 
		            	},formatter:function(value,row,index){
		            		if(value == 0) return '待审核';
		            		else if(value == 1) return '同意';
		            		else if(value == 2) return '不同意';
		            		return '';
		            	}"></th>
		            	<th data-options="field:'courseName',title:'课程名称',
		            	editor:{type:'textbox',required:true}"></th>
		            	<th data-options="field:'courseSubject',title:'课程学科',
		                editor:{
							type:'combobox',
							options:{
								valueField:'subject',
								textField:'subject',
								method:'get',
								url: '<%=basePath %>/subjectList1',
								mode:'remote',
								multiple:true,
								editable:false,
								required:true
							}
						}"></th>
		                <th data-options="field:'courseType',title:'课程类型',
		                editor:{type:'textbox',required:true}"></th>
		                <th data-options="field:'courseNo',title:'课程编号',
		                editor:{type:'textbox',required:true}"></th>
		                <th data-options="field:'classTeacherIds',title:'上课教师',
		                editor:{
							type:'combobox',
							options:{
								valueField:'id',
								textField:'name',
								method:'get',
								url: '<%=basePath %>/teacherAll',
								mode:'remote',
								multiple:true,
								editable:false,
								required:true
							}
						},formatter : function(value,row,index){
		                	return row.classTeacher;
		                }"></th>
		                <th data-options="field:'classAddr',title:'上课地址',
		                editor:{type:'textbox',required:true}"></th>
		                <th data-options="field:'classDate',title:'上课时间',
		                editor:{
		            		type:'combobox',
							options: {  
			                    method:'get',
								url: '<%=basePath %>/sys/course/manage/classDate',
								mode:'remote',
								multiple:true,
								editable:true
			                } 
		            	}"></th>
		                <th data-options="field:'limitNum',title:'限制人数',
		                editor:{type:'numberbox',required:true}"></th>
		                <th data-options="field:'courseDesc',title:'课程描述',multiline:true,
		                editor:{type:'textbox'},
		                formatter: function(value, row, index) {
		                    return '<span class=\'note\' title=\''+value+'\'>'+ value + '</span>';
		                }"></th>
		                <th data-options="field:'createTime',title:'创建时间',formatter: function(value, row, index) {
		                	return getTime(value,'yyyy-MM-dd hh:mm:ss');
		                }"></th>
		                <th data-options="field:'approver',title:'审核人'"></th>
		                <th data-options="field:'updateTime',title:'修改时间',formatter: function(value, row, index) {
		                	return getTime(value,'yyyy-MM-dd hh:mm:ss');
		                }"></th>
		            </tr>
		        </thead>
		    </table>
		</div>
	</div>
	
	<div id="uploadBox" style="display: none;height: 'auto';">
	    <div class="col-md-12 pd20">
   			<form id="uploadForm" novalidate="novalidate" enctype="multipart/form-data" method="post">
				<div class="form-group col-md-12" id="scoreTips">
					
				</div>
				<div class="form-group col-md-12 mt20">
					<input class="easyui-filebox form-control" id="files" name="files" style="width:250px;">
				</div>
			</form>
		</div>
	    <div class="text-center pd20">
	        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="uploadCommit()" style="width: 200px;">上传</a>
	    </div>
	</div>
	
	<jsp:include page="/WEB-INF/page/auth/_footer.jsp"/>
	<script src="<%=basePath %>/js/datagrid-helper.js" type="text/javascript"></script>
	<script type="text/javascript">
	 	var url = basePath + "/sys/course/manage";
        $(function(){
        	loadDatagrid("kccs_course_manage", url);
            $('#files').filebox({
                buttonText: '选择文件',
                buttonAlign: 'left'
            })
        })
        function loadDatagrid(cf, url){
        	dgEditIndex[cf]=-1;
        	var btnArr = [];
        	btnArr.push({text:'修改',iconCls:'icon-edit',handler:function(){modify(cf)}},'-');
       		btnArr.push({text:'保存',iconCls:'icon-save',handler:function(){save(cf)}},'-');
       		btnArr.push({text:'取消',iconCls:'icon-undo',handler:function(){undo(cf)}},'-');	
        	btnArr.push({text:'同意',iconCls:'icon-ok',handler:function(){ok(cf,url)}});
        	btnArr.push('-',{text:'不同意',iconCls:'icon-no',handler:function(){no(cf,url)}});
        	$("#"+cf+"dg").datagrid({
        		url: url + "/pageQuery",
        		rownumbers: true,
        		striped: true,
        		border: false,
        		pagination: true,
        		pageSize: 30,
        		fitColumns: true,
        		autoRowHeight: false,
        		singleSelect: false,
        		fit: true,
        		toolbar: btnArr,
        	    onAfterEdit: function(rowIndex,rowData,changes){
        	    	if($.isEmptyObject(changes)) {
        	    		var dgObj=$('#'+cf+'dg');
        				dgObj.datagrid('rejectChanges');
        				dgEditIndex[cf]=-1;
        				return;	
        			}
        			changes.id = rowData.id;
        	    	addmodify(changes, cf, url)
        	    },
        		onLoadSuccess: function(data){
        	        $(".note").tooltip({
        		        onShow: function () {
        			        $(this).tooltip('tip').css({
        			       		borderColor: '#000'
        			        });
        		        }
        	        });
        		}
        	})
        	var tb = $("#"+cf+"tb").html();
        	if(tb) {
        		$(".datagrid-toolbar").prepend(tb);
        		$("#"+cf+"tb").remove();
        	}
        }
        var uploadBox;
		function upload() {
			uploadBox = $('#uploadBox').dialog({
				title : '上传课程分类',
				modal : true,
				width : 400,
				top : 200,
				draggable : true,
				resizable : true,
				buttons : '#btns',
				onClose : function() {
					$('#files').filebox('setValue','');
				}
			}).show();
		}
		
		function uploadCommit(){
			var fileObj = document.getElementById('filebox_file_id_2').files[0];
			if(!fileObj) {
				$.messager.alert('提醒','请选择文件');
				return;
			}
			$.messager.progress({
				title : '提示',
				msg : '上传中，请稍候...'
			});
			var xhr = new XMLHttpRequest();
			xhr.open('POST', url + "/import");
			xhr.onload = function () {
		       if (xhr.status === 200) {
		    	    $.messager.progress('close');
			       	var resp = JSON.parse(xhr.response);
			       	if(resp.code == '000') {
			       		$.messager.alert("成功","上传成功！","info");
			       		reload("kccs_course_manage");
			       		$('#files').filebox('setValue','');
			       		uploadBox.dialog('close');
			       	}else {
			       		$.messager.alert('错误提醒', resp.msg);
			       	}
		       }
		   };
		   xhr.onerror = function () {
			   $.messager.progress('close');
			   $.messager.alert('错误', "上传异常");
		   };
		   var fd = new FormData();
		   fd.append('files', fileObj);
		   // 触发上传
			setTimeout(function() {
				xhr.send(fd);
			}, 1000)
		}
		function ok(cf,url){
			if(dgEditIndex[cf]>-1){
				$.messager.alert('提示','请先保存或取消编辑！','info');
			}else{
				var dgObj=$('#'+cf+'dg');
				var rows=dgObj.datagrid('getSelections');
				if(rows.length>0){
					$.messager.confirm("请确认","你确定同意已选择的课程吗？",function(b){
						if(b){
							var delIds=[];
							for(var i=0;i<rows.length;i++){
								delIds.push(rows[i].id);
							}
							$.ajax({
							   type:"POST",
							   url: url + "/openClass",
							   data:{courseIds:delIds.join(",")},
							   success: function(r){
							   		if(r.code == '000') {
							   			$.messager.alert('提示','提交成功！','info');
							   			dgObj.datagrid('reload');
							   			return;
							   		}
								    $.messager.alert('提示', "失败",'error');
								    undo(cf);
							    }
							});
						}
					})
				}else{
					$.messager.alert('提示','请选择行！','error');
				}
			}
		}
		
		function no(cf,url){
			if(dgEditIndex[cf]>-1){
				$.messager.alert('提示','请先保存或取消编辑！','info');
			}else{
				var dgObj=$('#'+cf+'dg');
				var rows=dgObj.datagrid('getSelections');
				if(rows.length>0){
					$.messager.confirm("请确认","你确定不同意已选择的课程吗？",function(b){
						if(b){
							var delIds=[];
							for(var i=0;i<rows.length;i++){
								delIds.push(rows[i].id);
							}
							$.ajax({
							   type:"POST",
							   url: url + "/stopClass",
							   data:{courseIds:delIds.join(",")},
							   success: function(r){
							   		if(r.code == '000') {
							   			$.messager.alert('提示','提交成功！','info');
							   			dgObj.datagrid('reload');
							   			return;
							   		}
								    $.messager.alert('提示', "失败",'error');
								    undo(cf);
							    }
							});
						}
					})
				}else{
					$.messager.alert('提示','请选择行！','error');
				}
			}
		}
    </script>
</body>
</html>